/**
*      author: Y_Yao
*      created: 2025-04-02 17:31:44
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 300 + 5;
int dp[N][N];//i为根选j个节点的最大值
int n,m;
vector<int> G[N];
int vis[N][N];
map<LL,LL> mp;

int dfs1(int num,int u,int step)
{
    if(G[u].size() == 0) return 0;
    int v = G[u][step];
    int ans = 0;
    if(step == G[u].size() - 1) return dp[v][num];
    if(vis[num][step]) return vis[num][step];
    for(int i = 0;i <= num;i++)
    {
        if(i && dp[v][i] == 0) break;
        ans = max(ans,dp[v][i] + dfs1(num-i,u,step+1));
    }
    return vis[num][step] = ans;
}

void dfs(int u)
{
    //先到根，归的时候填表
    for(int i = 0;i < G[u].size();i++)
    {
        dfs(G[u][i]);
    }
    memset(vis,0,sizeof vis);
    for(int i = 2;i <= m + 1;i++)
    {
        int ans = dp[u][1] + dfs1(i - 1,u,0);
        if(ans <= dp[u][i-1]) break;
        else dp[u][i] = ans;
    }
}

void solve()
{
    mp[1000];
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
    {
        int u,s;
        cin >> u >> s;
        G[u].push_back(i);
        dp[i][1] = s;
    }
    dfs(0);
    cout << dp[0][m+1] << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}